package com.controller;

import com.entity.PageResult;
import com.entity.QueryReaderPageBean;

import com.entity.Result;

import com.entity.User;
import com.service.AdminReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminReaderController {

    @Autowired
    AdminReaderService adminReaderService;

    //添加用户
    @RequestMapping("addUser")
    public Result addUser(@RequestBody User user){
//        System.out.println(user);
        try{
            adminReaderService.addUser(user);
            return new Result(true,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"添加失败");
        }

    }

    //得到所用用户
    @RequestMapping("getReaders")
    public PageResult getReaders(@RequestBody QueryReaderPageBean queryReaderPageBean){
        PageResult pageResult= adminReaderService.getAllReaders(queryReaderPageBean);

        return pageResult;
    }

    //查看操作
    @RequestMapping("getBorrowBooks")
    public Result  getBorrowBooks(Integer uNo){
        Result result=adminReaderService.getBorrowBooks(uNo);
        return result;
    }

    //得到用户个人信息
    @RequestMapping("getReaderDetail")
    public Result getReaderDetail(Integer uNo){
        Result result=adminReaderService.getReaderDetail(uNo);
        return result;
    }

    //修改用户信息
    @RequestMapping("UpdateReader")
    public Result updateReader(@RequestBody User user){
        try{
            adminReaderService.doUpdate(user);
            return new Result(true,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"修改失败");
        }
    }

    //挂失
    @RequestMapping("doLose")
    public Result doLost(Integer uNo,String uPassWord){

        try {
            adminReaderService.doLose(uNo,uPassWord);
            return new Result(true,"挂失成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"挂失失败");
        }

    }
}
